/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CLIENT;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

/**
 *The Main Frame For The Program
 * @author Yaniv Kalif
 */
public class ClientrMainChatFrame extends JFrame implements WindowListener,ActionListener
{
    ClientMainChatPanel _mainChatP;
    Image _image3DP;
    JMenuBar _menu;
    JMenuItem _exit;
    JMenu _changeTheme;
    JMenuItem _Theme_Green;
    JMenuItem _Theme_Blue;
    JMenuItem _Theme_Red;
    JMenuItem _Theme_Office;
    JMenuItem _Change_font;

    //CONNECTIONS
    Socket clientSocket = null;
    ClientConnection _client;
    /**
     * Default Constructor for ClientrMainChatFrame
     * This Constructor:
     * * set the icon image of the frame
     * * creates the MenuBar for this Frame
     * * creates a new MainChatPanel 
     */
    public ClientrMainChatFrame(String IP,int Port) throws IOException
    {  
        this.setResizable(false);//the window size cannot be changed
         //setting the Layout Manager
            BorderLayout layout=new BorderLayout();
            this.getContentPane().setLayout(layout);
        // Read Image
            _image3DP = Toolkit.getDefaultToolkit().getImage("Pics/3D Postman.gif");
        //create the menu bar
            _menu=new JMenuBar();
            _menu.setBorderPainted(true);
            //create the JMenus
                JMenu _file=new JMenu("File");
                JMenu _options=new JMenu("Options");
                _menu.add(_file);
                _menu.add(_options);
                //createMenu Items
                    //file menu
                        _exit=new JMenuItem("Exit");
                        _exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_DOWN_MASK ));//set shortcut
                        _exit.addActionListener(this);
                        _file.add(_exit);//add "exit" to "file"
                    //options menu
                        //Change theme item
                            _changeTheme=new JMenu("Change Theme");
                            _Theme_Green = new JMenuItem("Green Theme");
                            _Theme_Green.addActionListener(this);
                            _Theme_Green.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_DOWN_MASK ));//set shortcut
                            _Theme_Blue = new JMenuItem("Blue Theme");
                            _Theme_Blue.addActionListener(this);
                            _Theme_Blue.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_DOWN_MASK ));//set shortcut
                            _Theme_Red = new JMenuItem("red Theme");
                            _Theme_Red.addActionListener(this);
                            _Theme_Red.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_DOWN_MASK ));//set shortcut
                            _Theme_Office = new JMenuItem("The Office Theme");
                            _Theme_Office.addActionListener(this);
                            _Theme_Office.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_DOWN_MASK ));//set shortcut
                            _changeTheme.add(_Theme_Green);
                            _changeTheme.add(_Theme_Blue);
                            _changeTheme.add(_Theme_Red);
                            _changeTheme.add(_Theme_Office);
                            _options.add(_changeTheme);//add "Change Theme" to "Options"
                       //Change font item
                            _Change_font=new JMenuItem("Change Font");
                            _Change_font.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_DOWN_MASK ));//set shortcut
                            _Change_font.addActionListener(this);
                            _options.add(_Change_font);//add "change font" to "options"


                this.getContentPane().add(_menu,BorderLayout.PAGE_START);
    //create a new main chat panel
                //open client Socket
                _client = new ClientConnection();
                _client.Connect(IP, Port);
                clientSocket = _client.GetConnectedSocket();

    //add client main chat Panel
                _mainChatP = new ClientMainChatPanel(_client);
                this.getContentPane().add(_mainChatP);
           
        this.setIconImage(_image3DP); //add the picture as an Frame icon
        this.setTitle("Talk2Me - Client");
        this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
        this.addWindowListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {

        //******Menu button events***********//
            if(e.getSource()==_exit)//if the exit menu button pressed
                System.exit(1);
                //Change Theme buttons
                     if(e.getSource()==_Theme_Green)//if the Theme 1 menu button pressed (Green theme)
                    {
                        _mainChatP.ChangeTheme(new Color(60,179,113),new Color(152, 251, 152));
                    }
                    if(e.getSource()==_Theme_Blue)//if the Theme 2 menu button pressed (blue theme)
                    {
                        _mainChatP.ChangeTheme(Color.blue, Color.cyan);
                    }
                    if(e.getSource()==_Theme_Red)//if the Theme 3 menu button pressed (red theme)
                    {
                        _mainChatP.ChangeTheme(Color.RED, Color.pink);
                    }
                    if(e.getSource()==_Theme_Office)//if the Theme 4 menu button pressed (office theme)
                    {
                        _mainChatP.ChangeTheme(Color.black, Color.LIGHT_GRAY);
                    }
               //Change Font Button
                    if(e.getSource()==_Change_font)
                    {
                        // THE CODE TO CHANGE FONT - OPTIONAL
                    }

        
    }
    public void windowOpened(WindowEvent e) {}
    public void windowClosing(WindowEvent e) 
    {
        try
        {
            clientSocket.close();//close the server socket
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(ClientrMainChatFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.exit(1);
    }
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e){}
    public void windowActivated(WindowEvent e){}
    public void windowDeactivated(WindowEvent e){}


}

